package org.jtb.quickr;

import java.io.InputStream;
import org.apache.log4j.Logger;
import org.jdom.Element;

/**
 * User response.
 *
 * @see org.jtb.quickr.PeopleService#findByUserName(String)
 * @see org.jtb.quickr.PeopleService#findByEmail(String)
 * @see org.jtb.quickr.PeopleService#getUploadStatus(AuthResponse.Token,String)
 */
public class UserResponse extends FlickrResponse {
    /**
     * User bandwidth response.
     */
    public static class Bandwidth extends FlickrResponse {
        private long maxBytes;
        private int  maxKBytes;
        private long usedBytes;
        private int  usedKBytes;
        private long remainingBytes;
        private int  remainingKBytes;
        
        Bandwidth(Element e) throws QuickrException {
            super(e);
        }
        
        protected void populate(Element e) {
            Element bwElement = e.getChild("bandwidth");
            
            this.maxBytes = getAttrLong(bwElement, "maxbytes");
            this.maxKBytes = getAttrInt(bwElement, "maxkbytes");
            this.usedBytes = getAttrLong(bwElement, "usedbytes");
            this.usedKBytes = getAttrInt(bwElement, "usedkbytes");
            this.remainingBytes = getAttrLong(bwElement, "remainingbytes");
            this.remainingKBytes = getAttrInt(bwElement, "remainingkbytes");
        }
        
        public long getMaxBytes() {
            return maxBytes;
        }
        
        public int getMaxKBytes() {
            return maxKBytes;
        }
        
        public long getUsedBytes() {
            return usedBytes;
        }
        
        public int getUsedKBytes() {
            return usedKBytes;
        }
        
        public long getRemainingBytes() {
            return remainingBytes;
        }
        
        public int getRemainingKBytes() {
            return remainingKBytes;
        }
    }
    
    /**
     * User filesize response.
     */
    public static class Filesize extends FlickrResponse {
        private long maxBytes;
        private int  maxKBytes;
        
        Filesize(Element e) throws QuickrException {
            super(e);
        }
        
        protected void populate(Element e) {
            Element filesizeElement = e.getChild("filesize");
            
            this.maxBytes = getAttrLong(filesizeElement, "maxbytes");
            this.maxKBytes = getAttrInt(filesizeElement, "maxkbytes");
        }
        
        public long getMaxBytes() {
            return maxBytes;
        }
        
        public int getMaxKBytes() {
            return maxKBytes;
        }
    }
    
    /**
     * User sets response.
     */
    public static class Sets extends FlickrResponse {
        private int created;
        private int remaining;
        
        Sets(Element e) throws QuickrException {
            super(e);
        }
        
        protected void populate(Element e) {
            Element setsElement = e.getChild("sets");
            
            this.created = getAttrInt(setsElement, "created");
            try {
                this.remaining = getAttrInt(setsElement, "remaining");
            } catch (NumberFormatException nfe) {
                this.remaining = Integer.MAX_VALUE;
            }
        }

        public int getCreated() {
            return created;
        }

        public int getRemaining() {
            return remaining;
        }
    }
    
    private static Logger logger = Logger.getLogger(UserResponse.class.getName());
    
    private String nsId;
    private String userName;
    private boolean pro;
    private Bandwidth bandwidth;
    private Sets sets;
    private Filesize filesize;
    
    UserResponse(InputStream is) throws QuickrException {
        super(is);
    }
    
    protected void populate(Element e) throws QuickrException {
        Element userElement = e.getChild("user");
        
        this.nsId = userElement.getAttributeValue("nsid");
        this.userName = getBodyString(userElement, "username");
        this.pro = getAttrBoolean(e, "ispro");
        
        Element bandwidthElement = e.getChild("bandwidth");
        if (bandwidthElement != null) {
            this.bandwidth = new Bandwidth(bandwidthElement);
        }

        Element filesizeElement = e.getChild("filesize");
        if (filesizeElement != null) {
            this.filesize = new Filesize(filesizeElement);
        }
        
        Element setsElement = e.getChild("sets");
        if (setsElement != null) {
            this.sets = new Sets(setsElement);
        }
    }
    
    /**
     * Get the nsid.
     * @return The nsid.
     */
    public String getNsId() {
        return nsId;
    }
    
    private void setNsId(String nsId) {
        this.nsId = nsId;
    }
    
    /**
     * Get the user name.
     * @return The user name.
     */
    public String getUserName() {
        return userName;
    }
    
    private void setUserName(String userName) {
        this.userName = userName;
    }
    
    public boolean isPro() {
        return pro;
    }
}
